var cf = require("../../config.js");
var util = require("../../utils/util.js");
var cusmallToken = wx.getStorageSync('cusmallToken');
var baseHandle = require("../template/baseHandle.js");
var app = getApp();
Page(Object.assign({}, baseHandle, {

  /**
   * 页面的初始数据
   */
  data: {
    extConfig: wx.getExtConfigSync ? wx.getExtConfigSync() : {},
    app: app,
    staticResPath: cf.config.staticResPath,
    userImagePath: cf.config.userImagePath,
    wxBind:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var ctx = this;
    app.getUserInfo(this, options, function (userInfo, res) {
      cusmallToken = wx.getStorageSync('cusmallToken');
      var employee = wx.getStorageSync('employee');
      //var scene = decodeURIComponent(options.scene);
      ctx.setData({
        //eId:options.eId,
        employee: employee
      });
      ctx.getEmployeeInfo();
      
      // util.fetchSceneInfo(scene, ctx.infoCb)
    });


  },
  infoCb(res){
    var data = res.data;

  },
  wxBind(e){
    var ctx = this;
    var name = e.detail.value.name;
    var tel = e.detail.value.tel;
    console.log(e);
    if ("" == name || 11 != tel.length){
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: "您输入的员工信息有误，请核对后输入"
      })
      return;
    }
    wx.showLoading({
      title: '绑定中'
    });
    wx.request({
      url: cf.config.pageDomain + "/applet/mobile/visitcard/employee/wxBind",
      data: {
        cusmallToken: cusmallToken,
        name: name,
        phone: tel
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data;

        ctx.getBindEmployeeInfo(function(){
          if (data && 0 === data.ret) {
            wx.showModal({
              title: '提示',
              confirmText: app.globalData.cardTitle,
              showCancel: false,
              content: "绑定成功",
              success: function (res) {
                if (res.confirm) {
                  wx.redirectTo({
                    url: '/pages/sales/radar/radar',
                  })
                }
              }
            })
          } else {
            if (0 <= data.msg.indexOf("已绑定")) {

              wx.showModal({
                title: '提示',
                showCancel: false,
                content: "您已经是销售员，无需重复绑定！",
                success: function (res) {
                  if (res.confirm) {
                    wx.redirectTo({
                      url: '/pages/sales/radar/radar',
                    })
                  }
                }
              });

            } else {
              wx.showModal({
                title: '提示',
                showCancel: false,
                content: data.msg,
              })
            }

          }
        })
        
      },
      fail() {

      },
      complete() {
        wx.hideLoading();
      }
    });
  },
  getEmployeeInfo(){
    var ctx = this;
    wx.showLoading({
      title: '加载中'
    });
    wx.request({
      url: cf.config.pageDomain + "/applet/mobile/visitcard/employee/getEmployeeInfo",
      data: {
        cusmallToken: cusmallToken,
        //eid: ctx.data.eId
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data;
        if (data && 0 === data.ret) {
          if (data.model.employee && data.model.employee.wxBind){
            let openAi = (data.model.employee.powerSwitch & (Math.pow(2, 0))) != 0;
            //if (ctx.data.employee && data.model.employee.id == ctx.data.employee.id) {//判断当前的销售员 和 绑定的销售员是否是一个人
              wx.showModal({
                title: '提示',
                confirmText: "确定",
                showCancel: false,
                content: "已绑定",
                success: function (res) {
                  wx.redirectTo({
                    url: "/pages/sales/radar/radar",
                  })
                }
              });
            
            ctx.setData({
              wxBind: true
            })
          }else{
            ctx.setData({
              wxBind: false
            })
          }
          
        } else {
          wx.showToast({
            title: data.msg,
            icon:"none"
          });
        }
      },
      fail() {

      },
      complete() {
        wx.hideLoading();
      }
    });
  },
  //绑定后的 员工信息
  getBindEmployeeInfo(cb) {
    var ctx = this;
    wx.request({
      url: cf.config.pageDomain + "/applet/mobile/visitcard/employee/getEmployeeInfo",
      data: {
        cusmallToken: cusmallToken,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data;
        var employee = data.model && data.model.employee;
        wx.removeStorageSync("employee");
        wx.setStorageSync("employee", data.model.employee);
        cb && cb();
      },
      fail() {

      },
      complete() {
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}))